package com.uchedao.vehicledetection.utils;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.nostra13.universalimageloader.utils.L;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.inf.ICheckValueListener;

import java.lang.reflect.Field;
import java.util.Calendar;

public class CheckTextDateBtnValue {

	public static final int DATE_NORMAL = 0;// 正常时间
	public static final int DATE_PAST = 1;// 只能选择过去的时间
	public static final int DATE_FUTURE = 2;// 只能选择将来的时间

	public static final int TYPE_YEAR_MONTH_DAY = 0;// 年月日
	public static final int TYPE_YEAR_MONTH = 1;// 年月
	public static final int TYPE_YEAR = 2;// 年
	private DatePickerDialog datePickerDialog;
	private int type;// 显示类型
	private TextView tv;
	private Button btn;
	private ICheckValueListener listener;

	private int dateType;// 选择类型

	public CheckTextDateBtnValue(int type, ViewGroup viewGroup, int tvResId, int btnResId, String text,
			ICheckValueListener listener) {
		this(type, DATE_NORMAL, viewGroup, tvResId, btnResId, text, listener);
	}

	public CheckTextDateBtnValue(int type, int dateType, ViewGroup viewGroup, int tvResId, int btnResId, String text,
			ICheckValueListener listener) {
		this.type = type;
		this.dateType = dateType;
		Calendar calendar = Calendar.getInstance();
		datePickerDialog = new DatePickerDialog(viewGroup.getContext(), onDateSetListener, calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		if (type > TYPE_YEAR_MONTH_DAY)
			setDatePicker();
		this.listener = listener;
		tv = (TextView) viewGroup.findViewById(tvResId);
		btn = (Button) viewGroup.findViewById(btnResId);
		btn.addTextChangedListener(textStarWatcher);
		btn.setText(text);
		btn.setOnClickListener(buttonOnClickListener);
		if (!TextUtils.isEmpty(text) && listener != null)
			listener.getValue(btn, text);
	}

	private void setDatePicker() {
		try {
			DatePicker datePicker = datePickerDialog.getDatePicker();
			// Class<?> myDatePicker = null;
			// myDatePicker = Class.forName("android.widget.DatePicker");

			Field day = datePicker.getClass().getDeclaredField("mDaySpinner");
			day.setAccessible(true);
			NumberPicker mDaySpinner = (NumberPicker) day.get(datePicker);
			mDaySpinner.setEnabled(false);

			if (type > TYPE_YEAR_MONTH) {// 不能修改月
				Field month = datePicker.getClass().getDeclaredField("mMonthSpinner");
				month.setAccessible(true);
				NumberPicker mMonthSpinner = (NumberPicker) month.get(datePicker);
				mMonthSpinner.setEnabled(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 监听有值时显示星号
	 * 
	 * @author "叶晋"
	 * 
	 */
	TextWatcher textStarWatcher = new TextWatcher() {

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			L.i(tv + " " + s.toString());
			if (tv != null)
				setStar(tv, TextUtils.isEmpty(s));
		}

		/**
		 * 设置是否显示星号
		 * 
		 * @param tv
		 * @param show
		 */
		void setStar(TextView tv, boolean show) {
			tv.setCompoundDrawablesWithIntrinsicBounds(show ? R.mipmap.read_star : R.mipmap.read_star_no, 0, 0, 0);
		}
	};

	/**
	 * 按钮按下时
	 * 
	 * @author "叶晋"
	 * 
	 */
	OnClickListener buttonOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			datePickerDialog.show();
		}
	};

	OnDateSetListener onDateSetListener = new OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			Calendar calendar = Calendar.getInstance();
			int nowYear = calendar.get(Calendar.YEAR);
			int nowMonth = calendar.get(Calendar.MONTH);
			int nowDay = calendar.get(Calendar.DAY_OF_MONTH);

			if (dateType == DATE_PAST) {// 只能选择过去的时间
				if (year > nowYear) {// 年份大于当前年份
					year = nowYear;
					monthOfYear = nowMonth;
					dayOfMonth = nowDay;

				} else if (year == nowYear) {// 年份相同
					if (monthOfYear > nowMonth) {// 月份大于当前前
						monthOfYear = nowMonth;
						dayOfMonth = nowDay;
					} else if (monthOfYear == nowMonth) {// 月份相同，天数大于当前天
						if (dayOfMonth > nowDay)
							dayOfMonth = nowDay;
					}
				}
			}

			String value = year + "";
			if (type == TYPE_YEAR_MONTH) {
				value += "-" + (monthOfYear + 1);
			} else if (type == TYPE_YEAR_MONTH_DAY) {
				value += "-" + (monthOfYear + 1) + "-" + dayOfMonth;
			}
			btn.setText(value);
			if (listener != null)
				listener.getValue(btn, value);
		}
	};
}
